package com.na.tools.texturesplit;

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.table.AbstractTableModel;

public class TextureFrame {

	public static BufferedImage image;
	public String name;
	public int x, y, width, height;
	
	public TextureFrame(String name, int x, int y, int width, int height, boolean rotated) {
		this.name = name;
		this.x = x;
		this.y = y;
		this.width = rotated ? height : width;
		this.height = rotated ? width : height;
	}
	
	public static void setBaseImage(File image) {
		try {
			TextureFrame.image = ImageIO.read(image);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String get(int col) {
		if (col == 0) return name;
		if (col == 1) return String.valueOf(x);
		if (col == 2) return String.valueOf(y);
		if (col == 3) return String.valueOf(width);
		if (col == 4) return String.valueOf(height);
		return "";
	}
	
	public void paint(Graphics g, int x, int y, int width, int height) {
		System.out.println(x + "," + y + "," + width + "," + height);
		int dx1 = x;
		int dy1 = y;
		int dx2 = (int) (x + (float) width / this.width * this.width);
		int dy2 = (int) (y + (float) height / this.height * this.height);
		int sx1 = this.x;
		int sy1 = this.y;
		int sx2 = this.x + this.width;
		int sy2 = this.y = this.height;
		g.drawImage(image, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null);
		g.drawImage(image, x, y, x + width, y + height, null);
	}
	
	public void exportTo(File dir) throws IOException {
		File file = new File(dir, name);
		if (!file.exists()) {
			file.createNewFile();
		}
		BufferedImage buf = new BufferedImage(width, height, image.getType());
		buf.getGraphics().drawImage(image, 0, 0, width, height, x, y, x + width, y + height, null);
		ImageIO.write(buf, "png", file);
	}
	
	public static class TableModel extends AbstractTableModel implements Serializable {

		private static final long serialVersionUID = -2403664008027192186L;
		private List<TextureFrame> data = new ArrayList<TextureFrame>();
		private static final String[] COLS = { "name", "x", "y", "width", "height" };
		
		@Override
		public int getRowCount() {
			return data.size();
		}

		@Override
		public int getColumnCount() {
			return COLS.length;
		}
		
		@Override
		public String getColumnName(int column) {
			return COLS[column];
		}

		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			TextureFrame frame = data.get(rowIndex);
			return frame.get(columnIndex);
		}
		
		public void addRow(TextureFrame frame) {
			int size = data.size();
			data.add(frame);
			fireTableRowsInserted(size, size);
		}
		
		public void addRows(Collection<TextureFrame> frames) {
			int size = data.size();
			data.addAll(frames);
			fireTableRowsInserted(size, data.size() - 1);
		}
		
		public Collection<TextureFrame> getRowsData() {
			return Collections.unmodifiableCollection(data);
		}
		
		public void clearRowsData() {
			data.clear();
		}
		
	}
}
